﻿using MaiPlanServer.IServices.Production;
using MaiPlanServer.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System;
using MaiPlanServer.Model;

namespace MaiPlanServer.API.Controllers.Production
{
    [Route("api/[controller]")]
    [ApiController]
    public class TechnologyController : ControllerBase
    {
        public TechnologyController(ITechnologyServices db)
        {
            Db = db;
        }

        public ITechnologyServices Db { get; }
        /// <summary>
        /// 获取工艺路线
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="route_code"></param>
        /// <param name="route_name"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetPro_Routes(int PageIndex, int PageSize, string route_code, string route_name, string status)
        {
            try
            {
                var list = await Db.GetPro_Routes(PageIndex, PageSize, route_code, route_name, status);

                var count = await Db.GetPro_RoutesCount(route_code, route_name, status);

                return Ok(BaseState.CreateInstance(200, "请求成功", list, count));
            }
            catch (Exception ex)
            {

            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "服务器异常"));
        }
        /// <summary>
        /// 新增工艺路线
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        [HttpPost("AddPro_Routes")]
        public async Task<IActionResult> AddPro_Routes([FromForm] pro_route pro)
        {
            try
            {
                pro.route_id = Guid.NewGuid().ToString();
                bool bol = await Db.AddPro_Routes(pro);

                if (bol)
                {
                    return Ok(BaseState.CreateInstance(200, "新增成功"));
                }
            }
            catch (Exception ex)
            {

            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "服务器异常"));
        }
        /// <summary>
        /// 获取组成工序
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("GetPro_Route_Processes")]
        public async Task<IActionResult> GetPro_Route_Processes(string Id)
        {
            try
            {
                var list = await Db.GetPro_Route_Processes(Id);

                var data = await Db.GetPro_Routes(Id);

                return Ok(BaseState.CreateInstance(200, "请求成功",new { data, list }));
            }
            catch (Exception ex)
            {

            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "服务器异常"));
        }
        /// <summary>
        /// 获取所有工艺
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetALLPro_Processes")]
        public async Task<IActionResult> GetALLPro_Processes()
        {
            try
            {
                var list = await Db.GetALLPro_Processes();

                return Ok(BaseState.CreateInstance(200, "请求成功",list));
            }
            catch (Exception ex)
            {

            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "服务器异常"));
        }
        /// <summary>
        /// 修改工艺路线
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UpdatePro_Route([FromForm] pro_route pro)
        {
            try
            {
                bool bol = await Db.UpdatePro_Route(pro);

                if (bol)
                {
                    return Ok(BaseState.CreateInstance(200, "修改成功"));
                }
            }
            catch (Exception ex)
            {

            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "服务器异常"));
        }
        /// <summary>
        /// 新增工艺路线
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        [HttpPost("AddPro_Routes_Process")]
        public async Task<IActionResult> AddPro_Routes_Process([FromForm] pro_route_process pro)
        {
            try
            {
                pro.record_id = Guid.NewGuid().ToString();
                bool bol = await Db.AddPro_Routes_Process(pro);

                if (bol)
                {
                    return Ok(BaseState.CreateInstance(200, "新增成功"));
                }
            }
            catch (Exception ex)
            {

            }
            return Ok(BaseState.CreateInstance(BaseState.RequestError, "服务器异常"));
        }
    }
}
